﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace CPPEI.Coolzon.UI
{
    /*
    public abstract partial class PartContainer : Part
    {
        private List<Part> _partList;

        private Canvas _canvas;


        public List<Part> PartList
        {
            get
            {
                return _partList;
            }
        }

        public PartContainer()
        {
            _partList = new List<Part>();
            _canvas = new Canvas();
            //_canvas.Background = new SolidColorBrush(Colors.Yellow);
            this.Content = _canvas;
        }

        protected override void OnInit(PartContext context)
        {
            if (_partList != null)
            {
                for (int index = 0; index < _partList.Count; index++)
                {
                    _partList[index].Init(context);
                }
            }
            base.OnInit(context);
        }

        protected override void OnActive(PartContext context)
        {
            if (_partList != null)
            {
                for (int index = 0; index < _partList.Count; index++)
                {
                    _partList[index].Active(context);
                }
            }
            base.OnActive(context);
        }

        protected override void OnDeactive(PartContext context)
        {
            if (_partList != null)
            {
                for (int index = 0; index < _partList.Count; index++)
                {
                    _partList[index].Deactive();
                }
            }
            base.OnDeactive(context);
        }

        public void AddPart(Part part)
        {
            if (ValidateChildType(part.GetType()))
            {
                _partList.Add(part);
                _canvas.Children.Add(part);

                Canvas.SetLeft(part, part.XLeft);
                Canvas.SetTop(part, part.XTop);

                part.ParentPart = this;
                OnPartAdded(part);
            }
        }

        protected void SetPartLeft(Part part)
        {

        }

        public void SetPartTop(Part part)
        {
        }

        public void RemovePart(Part part)
        {
        }

        protected abstract void OnPartAdded(UI.Part part);

        public abstract bool ValidateChildType(Type childType);

        public override UI.Part GetPartAtPoint(Type type, Point pointInRoot, UIElement rootElement)
        {
            if (!XIsVisible)
            {
                return null;
            }
            UI.Part part = null;
           // Point absolutePoint = this.TranslatePoint(new Point(0, 0), rootElement);
            for (int index = 0; index < this._partList.Count; index++)
            {
                part = this._partList[index].GetPartAtPoint(type, pointInRoot, rootElement);
                if (part != null)
                {
                    return part;
                }
            }
            return base.GetPartAtPoint(type, pointInRoot, rootElement);
        }

        /// <summary>
        /// 获取子组件
        /// </summary>
        /// <param name="partName"></param>
        /// <param name="isRecursive">是否递归</param>
        /// <returns></returns>
        public UI.Part GetChildPart(string partName, bool isRecursive)
        {
            if (!XIsVisible)
            {
                return null;
            }
            UI.Part matchPart = null;
            if (isRecursive)
            {
                for (int index = 0; index < this._partList.Count; index++)
                {
                    UI.Part subPart = this._partList[index];
                    if (subPart.Name == partName)
                    {
                        matchPart = subPart;
                        break;
                    }
                    else if (subPart is UI.PartContainer)
                    {
                        matchPart = (subPart as UI.PartContainer).GetChildPart(partName, isRecursive);
                        if (matchPart != null)
                        {
                            break;
                        }
                    }
                    else if (subPart is UI.IPartContainer)
                    {
                        matchPart = (subPart as UI.IPartContainer).GetChildPart(partName, isRecursive);
                        if (matchPart != null)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                for (int index = 0; index < this._partList.Count; index++)
                {
                    UI.Part subPart = this._partList[index];
                    if (subPart.Name == partName)
                    {
                        matchPart = subPart;
                        break;
                    }
                }
            }
            return matchPart;
        }

        /// <summary>
        /// 获取实际的可存放组件的容器控件
        /// </summary>
        /// <returns></returns>
        public UIElement GetContentContainer()
        {
            return this._canvas;
        }

        public override void VisibleRegionChange(Point ltPtOfView, Point rbPtOfView)
        {
            for (int index = 0; index < this._partList.Count; index++)
            {
                this._partList[index].VisibleRegionChange(ltPtOfView, rbPtOfView);
            }

            base.VisibleRegionChange(ltPtOfView, rbPtOfView);
        }
    }
     * */

    public class PartCollection
    {
        private List<Part> _partList;
        public List<Part> PartList
        {
            get
            {
                return _partList;
            }
        }

        /// <summary>
        /// 所在的组件
        /// </summary>
        private Part _ownerPart;

        public int Count
        {
            get
            {
                return _partList.Count;
            }
        }

        public Part this[int index]
        {
            get
            {
                return _partList[index];
            }
        }

        public PartCollection(Part ownerPart)
        {
            _partList = new List<Part>();
            _ownerPart = ownerPart;
        }

        public void Add(Part part)
        {
            _partList.Add(part);
        }

        public void Insert(Part part, int index)
        {
            _partList.Insert(index, part);
        }

        public void Remove(Part part)
        {
            if (_partList.Contains(part))
            {
                _partList.Remove(part);
            }
        }

        public void Clear()
        {
            _partList.Clear();
        }

        /// <summary>
        /// 获取子组件
        /// </summary>
        /// <param name="partName"></param>
        /// <param name="isRecursive">是否递归</param>
        /// <returns></returns>
        public UI.Part GetChildPart(string partName, bool isRecursive)
        {
            bool ownerPartVisible = true;
            System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                ownerPartVisible = _ownerPart.XIsVisible;
            }));
            if (!ownerPartVisible)
            {
                return null;
            }
            UI.Part matchPart = null;

            if (isRecursive)
            {
                for (int index = 0; index < this._partList.Count; index++)
                {
                    UI.Part subPart = this._partList[index];
                    if (subPart.XFullName == partName)
                    {
                        matchPart = subPart;
                        break;
                    }
                    else if (subPart is UI.IPartContainer)
                    {
                        matchPart = (subPart as UI.IPartContainer).GetChildPart(partName, isRecursive);
                        if (matchPart != null)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                for (int index = 0; index < this._partList.Count; index++)
                {
                    UI.Part subPart = this._partList[index];
                    if (subPart.XFullName == partName)
                    {
                        matchPart = subPart;
                        break;
                    }
                }
            }
            return matchPart;
        }

        public List<T> GetChildPartByType<T>(bool isRecursive)
        {
            Type partType = typeof(T);
            List<T> partList = new List<T>();

            if (isRecursive)
            {
                for (int index = 0; index < this._partList.Count; index++)
                {
                    UI.Part part = this._partList[index];

                    if (partType.IsAssignableFrom(part.GetType()))
                    {
                        partList.Add((T)(object)part);
                    }

                    UI.IPartContainer container = part as IPartContainer;
                    if (container != null)
                    {
                        partList.AddRange(container.GetChildPartByType<T>(isRecursive));
                    }
                }
            }
            else
            {
                for (int index = 0; index < this._partList.Count; index++)
                {
                    UI.Part part = this._partList[index];

                    if (part.GetType().IsSubclassOf(partType))
                    {
                        partList.Add((T)(object)part);
                    }
                }
            }
            return partList;
        }
    }
}
